# -*- coding: utf-8 -*-

#   This file is part of emesene.
#
#    Emesene is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    emesene is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with emesene; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

VERSION = '0.1'
import Plugin

class MainClass(Plugin.Plugin):
    '''Main plugin class'''
    
    description = 'Commands to display html in conversations. '+\
    'Usage: /img <url>, /html <html code>'
    authors = { 'Roger Duran' : 'rogerduran@gmail.com' }
    website = 'http://www.rogerpc.com.ar'
    displayName = 'Html Commands'
    name = 'HtmlCommands'
    def __init__(self, controller, msn):
        '''Contructor'''
        
        Plugin.Plugin.__init__(self, controller, msn)
        
        self.description = 'Commands to display html in conversations. '+\
        'Usage: /img <url>, /html <html code>'
        self.authors = { 'Roger Duran' : 'rogerduran@gmail.com' }
        self.website = 'http://www.rogerpc.com.ar'
        self.displayName = 'Html Commands'
        self.name = 'HtmlCommands'
        self.controller = controller
        self.Slash = controller.Slash

    def start(self):
        '''start the plugin'''
        self.Slash.register('img', self.display_image, 'Display image')
        self.Slash.register('html', self.display_html, 'Display html code')
        self.enabled = True

    def display_image(self, slash_action):
        '''Display image in conversation'''
        img = slash_action.getParams()
        try:
            slash_action.conversation.ui.textview.display_html(
                                                        '<img src="%s"/>'%img)
        except:
            slash_action.outputText(_('Invalid url'))

    def display_html(self, slash_action):
        '''Display html in conversation'''
        html = slash_action.getParams()
        try:
            slash_action.conversation.ui.textview.display_html(
                                                        '<div>%s</div>'%html)
        except:
            slash_action.outputText(_('Invalid html code'))


    def stop(self):    
        '''stop the plugin'''
        self.Slash.unregister('img')
        self.Slash.unregister('html')
        self.enabled = False

    def check( self ):
        return ( True, 'Ok' )                                                        
